package cn.edu.tju.elm.controller;

import cn.edu.tju.elm.model.DTO.BusinessDTO;
import cn.edu.tju.elm.model.entity.Business;
import cn.edu.tju.elm.common.response.HttpResult;
import cn.edu.tju.elm.service.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/businesses")
@Tag(name = "店铺管理", description = "提供店铺的增删改查功能，包括根据用户token获取店铺、按类型筛选等")
@Slf4j
public class BusinessController {
    private final BusinessService businessService;

    public BusinessController(BusinessService businessService) {
        this.businessService = businessService;
    }

    @GetMapping("/users")
    @Operation(
            summary = "获取当前用户的店铺列表",
            description = "根据用户的JWT token，获取当前登录用户拥有的所有店铺信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = HttpResult.class))),
            @ApiResponse(responseCode = "401", description = "未授权访问")
    })
    public HttpResult<List<Business>> getBusinesses(){
        return HttpResult.ok(businessService.listBusinessesByToken());
    }

    @PostMapping("")
    @Operation(
            summary = "创建新店铺",
            description = "为当前登录用户创建一个新的店铺，需要商家权限(BUSINESS)"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "403", description = "权限不足，需要商家权限")
    })
    public HttpResult<Business> addBusiness(
            @Parameter(description = "店铺信息对象", required = true)
            @RequestBody Business business){
        return HttpResult.ok(businessService.addBusiness(business));
    }

    @GetMapping("/{id}")
    @Operation(
            summary = "根据ID获取店铺详情",
            description = "根据店铺ID获取指定店铺的详细信息"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "店铺不存在")
    })
    public HttpResult<Business> getBusiness(
            @Parameter(description = "店铺ID", required = true, example = "1")
            @PathVariable("id") Long id){
        return HttpResult.ok(businessService.getBusinessById(id));
    }

    @PutMapping("/{id}")
    @Operation(
            summary = "全量更新店铺信息",
            description = "全量更新指定店铺的所有信息，只能更新当前用户自己的店铺，且businessOwner和id字段无法更改"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "403", description = "只能更新自己的店铺"),
            @ApiResponse(responseCode = "404", description = "店铺不存在")
    })
    public HttpResult<Business> updateBusiness(
            @Parameter(description = "店铺ID", required = true, example = "1")
            @PathVariable("id") Long id,
            @Parameter(description = "店铺更新数据", required = true)
            @RequestBody BusinessDTO businessDTO){
        return HttpResult.ok(businessService.updateBusiness(id, businessDTO));
    }

    @PatchMapping("/{id}")
    @Operation(
            summary = "增量更新店铺信息",
            description = "部分更新店铺信息，只更新传入的字段，只能更新当前用户自己的店铺，且businessOwner字段无法更改"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "403", description = "只能更新自己的店铺"),
            @ApiResponse(responseCode = "404", description = "店铺不存在")
    })
    public HttpResult<Business> patchBusiness(
            @Parameter(description = "店铺ID", required = true, example = "1")
            @PathVariable("id") Long id,
            @Parameter(description = "店铺部分更新数据", required = true)
            @RequestBody BusinessDTO businessDTO){
        return HttpResult.ok(businessService.patchBusiness(id, businessDTO));
    }

    @GetMapping("/orderType/{typeId}")
    @Operation(
            summary = "根据订单类型筛选店铺",
            description = "根据店铺的订单类型ID(orderTypeId)筛选并返回符合条件的店铺列表"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "400", description = "请求参数无效")
    })
    public HttpResult<List<Business>> findBusinessesByOrderType(
            @Parameter(description = "订单类型ID", required = true, example = "1")
            @PathVariable("typeId") Integer type){
        return HttpResult.ok(businessService.listBusinessesByType(type));
    }

    @DeleteMapping("/{id}")
    @Operation(
            summary = "删除店铺",
            description = "删除指定的店铺，只能删除当前用户自己的店铺"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "403", description = "只能删除自己的店铺"),
            @ApiResponse(responseCode = "404", description = "店铺不存在")
    })
    public HttpResult<Business> deleteBusiness(
            @Parameter(description = "店铺ID", required = true, example = "1")
            @PathVariable("id") Long id){
        return HttpResult.ok(businessService.deleteBusiness(id));
    }
}